package com.bookmall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookmall.common.constant.MyStatusConstants;
import com.bookmall.member.domain.MemberLevel;
import com.bookmall.member.mapper.MemberLevelMapper;
import com.bookmall.member.service.IMemberLevelService;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author 胡菜鸡
 * @Create 2022-04-01-10:40
 */
@Service
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements IMemberLevelService {

    /**
     * 获取默认等级
     * @author 胡菜鸡
     * @date 2022/5/6 13:50
     */
    @Override
    public MemberLevel getDefaultLevel() {
        LambdaQueryWrapper<MemberLevel> wrapper = new LambdaQueryWrapper<>();
        MemberLevel memberLevel = baseMapper.selectOne(wrapper.eq(MemberLevel::getDefaultStatus, MyStatusConstants.DEFAULT));
        return memberLevel;
    }

    /**
     * 保存一个会员等级
     * @author 胡菜鸡
     * @date 2022/5/7 14:53
     */
    @Override
    public int saveMemberLevel(MemberLevel memberLevel) {
        if(memberLevel.getDefaultStatus() == MyStatusConstants.DEFAULT){
            if(getDefaultLevel() != null){
                throw new ServiceException("默认等级已存在");
            }
        }
        return save(memberLevel) ? 1 : 0;
    }
}
